@extends('layout.master')

@section('title','超市列表')

@section('css')
    @parent
    <!-- Daterange picker plugins css -->
    <link href="{{ asset('plugins/bower_components/bootstrap-daterangepicker/daterangepicker.css') }}" rel="stylesheet">
@endsection
@section('main')

    <!-- form itself -->
    <form id="add-market" class="mfp-hide white-popup-block floating-labels season-add" enctype="multipart/form-data"
          method="post">
        <h4>添加超市</h4>
        <hr>
        <div class="form-group m-b-40 m-t-10">
            <div class="form-group m-b-40 m-t-10">
                <input type="text" class="form-control" id="title" name="title"
                       required><span
                        class="highlight"></span> <span class="bar"></span>
                <label for="title">超市名称</label>
            </div>
            <div class="form-group m-b-40 m-t-10">
                <input type="text" class="form-control" id="mobile" name="mobile"
                       required><span
                        class="highlight"></span> <span class="bar"></span>
                <label for="mobile">超市联系方式</label>
            </div>
            <div class="form-group m-b-40 m-t-10">
                <p for="remarks">备注</p>
                <textarea id="remarks" name="remarks" cols="80" rows="5"></textarea>
            </div>
        </div>
        <div class="form-actions">
            <div class="row">
                <div class="col-md-12">
                    <div class="row">
                        <div class="col-md-offset-3 col-md-9">
                            <button type="submit" class="btn btn-success"><i
                                        class="fa fa-check"></i> 添加
                                {!! csrf_field() !!}
                            </button>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>


    <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel1">
        <div class="modal-dialog modal-lg" role="document">
            <div class="modal-content">
                <div class="modal-header">
                    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                    <h4 class="modal-title" id="exampleModalLabel1">修改</h4>
                </div>
                <div class="modal-body">
                    <form class="floating-labels role-edit">
                        <input id="id-modify" name="id" type="hidden" value="">
                        <fieldset style="border:0;">

                            <div class="form-group m-b-40 m-t-10">
                                <input type="text" class="form-control" id="title-modify" name="title" required><span
                                        class="highlight"></span> <span class="bar"></span>
                                <label for="title-modify">超市名称</label>
                            </div>
                            <div class="form-group m-b-40 m-t-10">
                                <input type="text" class="form-control" id="mobile-modify" name="mobile" required><span
                                        class="highlight"></span> <span class="bar"></span>
                                <label for="mobile-modify">联系方式</label>
                            </div>
                            <div class="form-group m-b-40 m-t-10">
                                <p for="remarks-mobidy">备注</p>
                                <textarea id="remarks-modify" name="remarks" cols="80" rows="5"></textarea>
                            </div>
                            <div class="form-actions">
                                <div class="row">
                                    <div class="col-md-12">
                                        <div class="row">
                                            <div class="col-md-offset-3 col-md-9">
                                                <button type="button" class="btn btn-default" data-dismiss="modal">取消
                                                </button>
                                                {!! csrf_field() !!}
                                                <button type="submit" class="btn btn-success" id="btn-modify"><i
                                                            class="fa fa-check"></i> 保存
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </fieldset>
                    </form>
                </div>
            </div>
        </div>
    </div>


    <div class="panel">
        <div class="panel-action" style="margin-top: -9px">
            <a class="popup-with-form btn fcbtn btn btn-outline btn-info btn-1c" href="#add-market">
                <span>添加超市</span>
            </a>

        </div>
        <div class="panel-wrapper collapse in" aria-expanded="true">
            <div class="panel-body">
                <form action="#">
                    <div class="form-body">
                        <div class="row">
                            <div class="col-md-3 col-sm-6">
                                <div class="form-group">
                                    <label class="control-label">时间</label>
                                    <div class="example">
                                        <input class="column_filter form-control input-daterange-datepicker" type="text"
                                               data-column="0" name="time"/>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
    <div class="panel">
        <div class="panel-wrapper collapse in" aria-expanded="true">
            <div class="panel-body">
                <div class="table-responsive">
                    <table id="example23" class="display nowrap" cellspacing="0" width="100%"
                           data-url="{{ url(request()->getUri()) }}">
                        <thead>
                        <tr>
                            <th>ID</th>
                            <th>二维码</th>
                            <th>商家查询二维码</th>
                            <th>超市名称</th>
                            <th>扫码用户数</th>
                            <th>下载人数</th>
                            <th>商家联系方式</th>
                            <th>备注</th>
                            <th>修改</th>
                        </tr>
                        </thead>
                        <tbody>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
@endsection
@section('js')
    @parent
    <!-- Plugin JavaScript -->
    <script src="{{ asset('plugins/bower_components/moment/moment.js') }}"></script>
    <!-- Date range Plugin JavaScript -->
    <script src="{{ asset('plugins/bower_components/bootstrap-daterangepicker/daterangepicker.js') }}"></script>
    <script src="{{ asset('plugins/bower_components/Magnific-Popup-master/dist/jquery.magnific-popup.min.js') }}"></script>
    <script src="{{ asset('plugins/bower_components/Magnific-Popup-master/dist/jquery.magnific-popup-init.js') }}"></script>

    <script>
        $('.input-daterange-datepicker').daterangepicker({});
    </script>
    <script src="{{ asset('js/myself.js') }}"></script>

    <script>
        $('#add-market').submit(function (e) {
            e.preventDefault();
            var title = $("#title").val();
            var mobile = $("#mobile").val();
            var remarks = $("#remarks").val();
            swal({
                    title: "",
                    text: "确定添加吗？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的",
                    closeOnConfirm: false
                },
                function () {
                    $.ajax({
                        type: "POST",
                        url: '/market/markets/publish',
                        data: {title: title, mobile: mobile, remarks: remarks},
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        error: function (err) {
                            for (var j in err.responseJSON) {
                                err.responseJSON[j].forEach(function (item, index) {
                                    swal(item);
                                })
                            }
                        },
                        success: function (data) {
                            if (data.status === 1) {
                                swal(data.message, '', 'success');
                                $('.confirm').click(function () {
                                    location.reload();
                                });
                            } else {
                                swal(data.message);
                            }
                        }
                    });
                }
            );
        });

        function modify(sid) {
            $.ajax({
                type: "POST",
                url: '/market/markets/info/' + sid,
                headers: {
                    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                },
                error: function (err) {
                    for (var j in err.responseJSON) {
                        err.responseJSON[j].forEach(function (item, index) {
                            swal(item);
                        })
                    }
                },
                success: function (data) {
                    $("#id-modify").val(sid);
                    $("#title-modify").val(data.title);
                    $("#mobile-modify").val(data.mobile);
                    $("#remarks-modify").val(data.remarks);
                }
            });
        }

        $('#btn-modify').click(function (e) {
            e.preventDefault();
            var id = $("#id-modify").val();
            var title = $("#title-modify").val();
            var mobile = $("#mobile-modify").val();
            var remarks = $("#remarks-modify").val();
            swal({
                    title: "",
                    text: "确定修改吗？",
                    type: "warning",
                    showCancelButton: true,
                    confirmButtonColor: "#DD6B55",
                    confirmButtonText: "是的",
                    closeOnConfirm: false
                },
                function () {
                    $.ajax({
                        type: "POST",
                        url: '/market/markets/modify/' + id,
                        data: {title: title, mobile: mobile, remarks: remarks},
                        headers: {
                            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
                        },
                        error: function (err) {
                            for (var j in err.responseJSON) {
                                err.responseJSON[j].forEach(function (item, index) {
                                    swal(item);
                                })
                            }
                        },
                        success: function (data) {
                            if (data.status === 1) {
                                swal(data.message, '', 'success');
                                $('.confirm').click(function () {
                                    location.reload();
                                });
                            } else {
                                swal(data.message);
                            }
                        }
                    });
                }
            );
        });


    </script>
@endsection